<template>
  <el-row :gutter="24" class="pl80">
    <el-col>
      <h2>
        教育经历
        <!-- <el-button size="small" @click="handleShowHistory">历史信息</el-button> -->
        <el-button type="primary" size="small" @click="handleEdit(false, {})" icon="el-icon-plus" />
      </h2>
    </el-col>
    <el-col>
      <e-table
        ref="eTable"
        @getList="handleGetList"
        :tableData="tableData"
        :columns="columns"
        :loading="loading"
        :pageObj="null"
      >
        <el-table-column slot="action" fixed="right" width="50" align="center">
          <template slot-scope="scope">
            <el-dropdown>
              <a class="el-dropdown-link">
                <i class="el-icon-menu"></i>
              </a>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item @click.native="handleEdit(true, scope.row)">
                  <a>修改</a>
                </el-dropdown-item>
                <el-dropdown-item @click.native="handleDelete(scope.row)">
                  <a class="red">删除</a>
                </el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </e-table>
    </el-col>
    <edu-modal ref="eduModal" @refresh="handleRefresh" />
  </el-row>
</template>

<script>
import { getUserEdu, deleteUserEdu } from '@/api/archives'
import { setSession, getSession } from '@/utils/util'

import EduModal from './EduModal'

export default {
  components: {
    EduModal
  },
  data() {
    return {
      loading: false,
      tableData: [],
      columns: [
        {
          label: '入学日期',
          prop: 'startDate'
        },
        {
          label: '毕业日期',
          prop: 'endDate'
        },
        {
          label: '毕业学校',
          prop: 'gradSchool'
        },
        {
          label: '专业',
          prop: 'speciality'
        },
        {
          label: '学历',
          prop: 'education'
        },
        {
          label: '学位',
          prop: 'degree'
        },
        {
          label: '学制',
          prop: 'eduSystem'
        },
        {
          label: '学位证书编号',
          prop: 'diplomaNo'
        },
        {
          label: '学历证书影印件',
          prop: 'diplomaPhotocopy'
        },
        {
          label: '备注',
          prop: 'remark'
        },
        { slot: 'action' }
      ],
      userRow: null
    }
  },
  methods: {
    handleRefresh() {
      this.handleGetList(this.userRow.userId)
    },
    handleShowHistory() {
      this.$emit('showHistory', 'edu')
    },
    handleEdit(bool, row) {
      this.$nextTick(() => {
        this.$refs.eduModal.edit(bool, row)
      })
    },
    handleDelete(row) {
      const _this = this
      this.$confirm({
        title: '删除操作',
        content: `您确定删除这条记录吗？`,
        okText: '确定',
        okType: 'danger',
        cancelText: '取消',
        centered: true,
        async onOk() {
          // 调用删除接口，刷新表格数据
          const res = await deleteUserEdu(row.userEduId, {})
          openNotification('success', '删除成功')
          _this.handleGetList(_this.userRow.userId)
        }
      })
    },
    async handleGetList(id, param = {}) {
      this.loading = true
      const res = await getUserEdu(id, param)
      if (res && res.data.length > 0) {
        const { state, data } = res
        this.tableData = data
      } else {
        this.tableData = []
      }
      this.loading = false
    }
  },
  created() {
    if (this.$route.query.userRow) {
      this.userRow = JSON.parse(this.$route.query.userRow)
    }
    if (this.userRow) {
      setSession('userRow', this.userRow)
    } else {
      this.userRow = getSession('userRow')
    }
    this.handleGetList(this.userRow.userId)
  }
}
</script>
